package com.zzyl;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class SpringBootTesta {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Test
    public void testRedisString(){
        ValueOperations<String, String> op = redisTemplate.opsForValue();
        op.set("大胃袋","味儿真足");
        String string = op.get("大胃袋");
        System.out.println(string);
        op.set("潦草的","这一块",10, TimeUnit.SECONDS);
        Boolean b = op.setIfAbsent("大胃袋", "这一块");
        System.out.println(b);
        Boolean b1 = op.setIfAbsent("你们根本就不懂", "这减肥这一块");
        System.out.println(b1);
    }
    @Test
    public void testRedisHash(){
        HashOperations<String, Object, Object> op = redisTemplate.opsForHash();
        op.put("myHash","a","1");
        op.put("myHash","b","2");
        Object o = op.get("myHash", "a");
        System.out.println(o);
        Set<Object> myHash = op.keys("myHash");
        System.out.println(myHash);
        List<Object> myHash1 = op.values("myHash");
        System.out.println(myHash1);
        op.delete("myHash","a");
    }
    @Test
    public void testRedisList(){
        ListOperations<String, String> op = redisTemplate.opsForList();
        op.leftPushAll("mylist","a","b","c");
        List<String> mylist = op.range("mylist", 0, 2);
        System.out.println(mylist);
        op.leftPop("mylist");
        Long mylist1 = op.size("mylist");
        System.out.println(mylist1);
    }
    @Test
    public void testRedisSet(){
        SetOperations<String, String> op = redisTemplate.opsForSet();
        op.add("myset", "良子", "旺仔小乔", "你们好吗", "我是旺仔小乔");
        Set<String> myset = op.members("myset");
        System.out.println(myset);
        Long myset1 = op.size("myset");
        System.out.println(myset1);
        op.add("myset1",  "你们好吗", "我是旺仔小乔","迪迦","赛文");
        Set<String> intersect = op.intersect("myset", "myset1");
        System.out.println(intersect);
        Set<String> union = op.union("myset", "myset1");
        System.out.println(union);
        Set<String> difference = op.difference("myset", "myset1");
        System.out.println(difference);

    }
    @Test
    public void testRedisZSet(){
        ZSetOperations<String, String> op = redisTemplate.opsForZSet();
        op.add("凹凸曼","迪迦",1);
        op.add("凹凸曼","赛文",2);
        op.add("凹凸曼","泰罗",3);
        Set<String> range = op.range("凹凸曼", 0, 2);
        System.out.println(range);
        Set<ZSetOperations.TypedTuple<String>> range1 = op.rangeWithScores("凹凸曼", 0, 2);
        for (ZSetOperations.TypedTuple<String> stringTypedTuple : range1) {
            stringTypedTuple.getValue();
            System.out.println(stringTypedTuple.getValue());
            stringTypedTuple.getScore();
            System.out.println(stringTypedTuple.getScore());
        }
        op.incrementScore("凹凸曼","赛文",10);
        op.remove("凹凸曼","迪迦");
        Long rank = op.rank("凹凸曼", "赛文");
        System.out.println(rank);
        Long l = op.reverseRank("凹凸曼", "赛文");
        System.out.println(l);
    }



}
